package com.zmcn.msg.business.phoneGroup.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zmcn.msg.bean.po.Phone;
import com.zmcn.msg.bean.po.PhoneGroup;
import com.zmcn.msg.bean.po.PhoneToGroup;
import com.zmcn.msg.bean.po.User;
import com.zmcn.msg.bean.vo.PhoneVO;
import com.zmcn.msg.business.phoneGroup.dao.PhoneGroupDao;
import com.zmcn.msg.business.phoneGroup.service.PhoneGroupService;
import com.zmcn.msg.business.phoneGroup.vo.PhoneGroupVO;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.util.FileOP;
import com.zmcn.msg.fw.util.ParseUtil;

/**
 * 
 * 分群信息业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class PhoneGroupServiceImpl implements PhoneGroupService {
	private PhoneGroupDao dao;

	@Autowired
	public void setDao(PhoneGroupDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取分群信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<PhoneGroupVO> getList(Page<PhoneGroupVO> page) {
		return dao.getList(page);
	}

	/**
	 * 获取群手机信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @return
	 */
	public Page<PhoneVO> getPhoneList(Page<PhoneVO> page) {
		return dao.getPhoneList(page);
	}

	/**
	 * 导入群信息
	 * 
	 * @param fileName
	 * @param file
	 */
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	public boolean imp(String fileName, File file) {
		boolean result = false;
		String userName = fileName.substring(0, fileName.lastIndexOf("."));
		List<User> users = (List<User>) dao.queryByHql("from " + User.class.getName() + " where name=?", new Object[] { userName });
		if (users.size() == 0) {
			return result;
		}
		String userId = users.get(0).getUserId();
		// 解析导入文件
		String c = FileOP.readFile(file, "gb2312");
		String[] line = c.split("\r\n");
		String tempGroup = "";
		Map<String, List<String>> mg = new HashMap<String, List<String>>();
		Map<String, Phone> mp = new HashMap<String, Phone>();
		for (int i = 0; i < line.length; i++) {
			String[] s = ParseUtil.splitByDot(line[i]);
			if (s.length >= 1 && null != s[0] && !"".equals(s[0])) {
				tempGroup = s[0];
			}
			if (s.length >= 3 && null != s[1] && !"".equals(s[1]) && null != s[2] && !"".equals(s[2])) {
				String phoneNo = s[2];
				if (!mp.containsKey(phoneNo)) {
					// 创建手机号码信息
					Phone p = new Phone();
					p.setUserId(userId);
					p.setName(s[1]);
					p.setPhoneNo(s[2]);
					dao.create(p);
					mp.put(phoneNo, p);
				}
				Phone tmp = mp.get(phoneNo);
				if (!mg.containsKey(tempGroup)) {
					List<String> list = new ArrayList<String>();
					list.add(tmp.getPhoneId());
					mg.put(tempGroup, list);
				} else {
					mg.get(tempGroup).add(tmp.getPhoneId());
				}

			}

		}
		// 创建群
		for (String groupName : mg.keySet()) {
			List<String> ps = mg.get(groupName);
			PhoneGroup pg = new PhoneGroup();
			pg.setGroupName(groupName);
			pg.setGroupDesc(groupName);
			pg.setCount(ps.size());
			pg.setUserId(userId);
			dao.create(pg);
			// 创建关系表
			for (String s : ps) {
				PhoneToGroup ptg = new PhoneToGroup();
				ptg.setGroupId(pg.getGroupId());
				ptg.setPhoneId(s);
				dao.create(ptg);
			}
		}
		result = true;
		return result;
	}
}
